package com.css.sword.knowing.search.aggregation;

import java.io.IOException;
import java.util.List;

import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;

import com.css.sword.knowing.search.SearchResult;
import com.css.sword.knowing.search.util.ToJson;

/**
 * 聚合检索结果类
 */
public class SearchAndAggResult {

    private List<SearchResult> searchResults;
    private AggResult aggResult;
    private String query; 
    private long pageNum;
    private long pageSize;

    /**
     * 获取聚合检索中的检索结果集
     * @return 检索结果集
     */
    public List<SearchResult> getSearchResults() {
        return searchResults;
    }

    /**
     * 设置聚合检索中的检索结果集
     * @param searchResults 检索结果集
     */
    public void setSearchResults(List<SearchResult> searchResults) {
        this.searchResults = searchResults;
    }

    /**
     * 获取聚合检索中聚合运算的结果集
     * @return 聚合运算的结果集AggResult
     */
    public AggResult getAggResult() {
        return aggResult;
    }

    /**
     * 设置聚合检索中聚合运算的结果集
     * @param aggResult 聚合检索中聚合运算的结果集AggResult
     */
    public void setAggResult(AggResult aggResult) {
        this.aggResult = aggResult;
    }
    
    /**
     * 获取检索条件
     * @return 检索条件的json串
     */
    public String getQuery() {
        return query;
    }
    
    /**
     * 设置获取检索条件
     * @param data
     *            获取检索条件的json串
     */
    public void setQuery(String query) {
        this.query = query;
    }

    /**
     * 返回当前页码
     * @return
     */
    public long getPageNum() {
		return pageNum;
	}

	/**
	 * 设置当前的页码
	 * 
	 * @param pageNum
	 */
	public void setPageNum(long pageNum) {
		this.pageNum = pageNum;
	}
	
    /**
     * 获取单页记录数
     * @return
     */
    public long getPageSize() {
		return pageSize;
	}

	/**
	 * 设置单页记录数
	 * @param pageSize
	 */
	public void setPageSize(long pageSize) {
		this.pageSize = pageSize;
	}
	
	 /**
     * 检索结果集转成json字符串格式
     * 
     * @return json字符串
	 * @throws IOException 
     */
	public String toJson() throws IOException {

		String ret = "";
		XContentBuilder jsonBuilder = XContentFactory.jsonBuilder().startObject();

		ToJson.getSearchResultJson(jsonBuilder, searchResults.get(0));

		ToJson.getAggResultJson(jsonBuilder, aggResult);

		jsonBuilder.endObject();
		ret = jsonBuilder.string();

		return ret;
	}
}
